import request from '../utils/request'

// 获取文章列表
export function getArticles(params) {
  return request({
    url: '/api/articles',
    method: 'get',
    params: {
      ...params,
      tag: params?.tag
    }
  })
}

// 获取文章详情
export function getArticleById(id) {
  return request({
    url: `/api/articles/${id}`,
    method: 'get'
  })
}

// 创建文章
export function createArticle(data) {
  // 确保数据格式符合后端要求
  const articleData = {
    title: data.title,
    content: data.content,
    tags: Array.isArray(data.tags) ? data.tags : []
  }

  return request({
    url: '/api/articles',
    method: 'post',
    data: articleData
  })
}

// 更新文章
export function updateArticle(id, data) {
  return request({
    url: `/api/articles/${id}`,
    method: 'put',
    data
  })
}

// 删除文章
export function deleteArticle(id) {
  return request({
    url: `/api/articles/${id}`,
    method: 'delete'
  })
}

// 点赞文章
export function likeArticle(id) {
  return request({
    url: `/api/articles/${id}/like`,
    method: 'post'
  })
}

// 获取文章评论
export function getArticleComments(articleId) {
  return request({
    url: `/api/articles/${articleId}/comments`,
    method: 'get'
  })
}

// 发表评论
export function createComment(articleId, data) {
  return request({
    url: `/api/articles/${articleId}/comments`,
    method: 'post',
    data
  })
} 